//
//  LCFNetWorkRequest.m
//  lcf-newproject
//
//  Created by yangyang on 14-11-17.
//  Copyright (c) 2014年 yang. All rights reserved.
//

#import "LCFNetWorkRequest.h"
#import "AFHTTPRequestOperationManager.h"
#import "RequestManager.h"
#import "SVProgressHUD.h"

static LCFNetWorkRequest * requestManager = nil;
@implementation LCFNetWorkRequest

+(LCFNetWorkRequest *)shareRequestManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        requestManager = [[LCFNetWorkRequest alloc]init];
    });
    return requestManager;
}

#pragma mark - 通用的请求方法
-(void)sendAsynchronousRequestDataWithUrl:(NSString *)url
                      parameters:(NSDictionary *)parameters
                   requestMethod:(LCFRequestMethods)methods
                completionHandle:(void(^)(ResponseItem * item))completionBlock
                   failedHandler:(void (^)(NSError *error))failedBlock{
    
    if (![[NetWorkConfig shareNetWorkConfig]checkNetWork]) {
        [SVProgressHUD showErrorWithStatus:NetError duration:NetTimeInterval];
        NSLog(@"网络连接错误");
        return;
    }
    [SVProgressHUD showWithStatus:NetIsLoading maskType:SVProgressHUDMaskTypeNone];
    if (methods == LCFRequestGetMethod){
        AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
        [[RequestManager shareRequestManager]setRequest:manager forKey:url];
        __block LCFNetWorkRequest * blockManager = self;
        [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            ResponseItem * item = [blockManager parseData:responseObject];
            completionBlock(item);
            [SVProgressHUD showSuccessWithStatus:NetLoadingSuccess duration:NetTimeInterval];
            [[RequestManager shareRequestManager]removeRequestForKey:url];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error = %@",error);
            failedBlock(error);
            [SVProgressHUD showErrorWithStatus:NetLoadingFailed duration:NetTimeInterval];
            [[RequestManager shareRequestManager]removeRequestForKey:url];
        }];
        
    }else if (methods == LCFRequestPostMethod){
        AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
        [[RequestManager shareRequestManager]setRequest:manager forKey:url];
        __block LCFNetWorkRequest * blockManager = self;
        [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            ResponseItem * item = [blockManager parseData:responseObject];
            completionBlock(item);
            [SVProgressHUD showSuccessWithStatus:NetLoadingSuccess duration:NetTimeInterval];
            [[RequestManager shareRequestManager]removeRequestForKey:url];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            failedBlock(error);
            [SVProgressHUD showErrorWithStatus:NetLoadingFailed duration:NetTimeInterval];
            [[RequestManager shareRequestManager]removeRequestForKey:url];
        }];
    }
}

//通用解析方法
-(ResponseItem *)parseData:(id)jsonData{
    ResponseItem *item = nil;
    if (jsonData) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        item = [[ResponseItem alloc]initWithData:dict];
    }
    return item;
}

-(void)cancelRequestWithUrl:(NSString *)url{
    
}

@end
